package org.gap.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.gap.domain.Adherent;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Adherent entities.
 * 
 */
@Repository("AdherentDAO")
@Transactional
public class AdherentDAOImpl extends AbstractJpaDao<Adherent> implements
		AdherentDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Adherent.class }));

	/**
	 * EntityManager injected by Spring for persistence unit 
	 *
	 */
	@PersistenceContext(unitName = "")
	private EntityManager entityManager;

	/**
	 * Instantiates a new AdherentDAOImpl
	 *
	 */
	public AdherentDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findAdherentByAdhAffiliation
	 *
	 */
	@Transactional
	public Set<Adherent> findAdherentByAdhAffiliation(Integer adhAffiliation) throws DataAccessException {

		return findAdherentByAdhAffiliation(adhAffiliation, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhAffiliation
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAdherentByAdhAffiliation(Integer adhAffiliation, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdherentByAdhAffiliation", startResult, maxRows, adhAffiliation);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * JPQL Query - findAdherentByAdhDateAffiliationBefore
	 *
	 */
	@Transactional
	public Set<Adherent> findAdherentByAdhDateAffiliationBefore(java.util.Calendar adhDateAffiliation) throws DataAccessException {

		return findAdherentByAdhDateAffiliationBefore(adhDateAffiliation, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhDateAffiliationBefore
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAdherentByAdhDateAffiliationBefore(java.util.Calendar adhDateAffiliation, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdherentByAdhDateAffiliationBefore", startResult, maxRows, adhDateAffiliation);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * JPQL Query - findAdherentByAdhCin
	 *
	 */
	@Transactional
	public Set<Adherent> findAdherentByAdhCin(Integer adhCin) throws DataAccessException {

		return findAdherentByAdhCin(adhCin, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhCin
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAdherentByAdhCin(Integer adhCin, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdherentByAdhCin", startResult, maxRows, adhCin);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * JPQL Query - findAdherentByAdhDateAffiliation
	 *
	 */
	@Transactional
	public Set<Adherent> findAdherentByAdhDateAffiliation(java.util.Calendar adhDateAffiliation) throws DataAccessException {

		return findAdherentByAdhDateAffiliation(adhDateAffiliation, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhDateAffiliation
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAdherentByAdhDateAffiliation(java.util.Calendar adhDateAffiliation, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdherentByAdhDateAffiliation", startResult, maxRows, adhDateAffiliation);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * JPQL Query - findAdherentByAdhDateAffiliationAfter
	 *
	 */
	@Transactional
	public Set<Adherent> findAdherentByAdhDateAffiliationAfter(java.util.Calendar adhDateAffiliation) throws DataAccessException {

		return findAdherentByAdhDateAffiliationAfter(adhDateAffiliation, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhDateAffiliationAfter
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAdherentByAdhDateAffiliationAfter(java.util.Calendar adhDateAffiliation, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdherentByAdhDateAffiliationAfter", startResult, maxRows, adhDateAffiliation);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * JPQL Query - findAdherentByAdhPension
	 *
	 */
	@Transactional
	public Set<Adherent> findAdherentByAdhPension(Integer adhPension) throws DataAccessException {

		return findAdherentByAdhPension(adhPension, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhPension
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAdherentByAdhPension(Integer adhPension, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdherentByAdhPension", startResult, maxRows, adhPension);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * JPQL Query - findAdherentByAdhSom
	 *
	 */
	@Transactional
	public Set<Adherent> findAdherentByAdhSom(Integer adhSom) throws DataAccessException {

		return findAdherentByAdhSom(adhSom, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhSom
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAdherentByAdhSom(Integer adhSom, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdherentByAdhSom", startResult, maxRows, adhSom);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllAdherents
	 *
	 */
	@Transactional
	public Set<Adherent> findAllAdherents() throws DataAccessException {

		return findAllAdherents(-1, -1);
	}

	/**
	 * JPQL Query - findAllAdherents
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAllAdherents(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAdherents", startResult, maxRows);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * JPQL Query - findAdherentByPrimaryKey
	 *
	 */
	@Transactional
	public Adherent findAdherentByPrimaryKey(Integer adhId) throws DataAccessException {

		return findAdherentByPrimaryKey(adhId, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByPrimaryKey
	 *
	 */

	@Transactional
	public Adherent findAdherentByPrimaryKey(Integer adhId, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findAdherentByPrimaryKey", adhId);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAdherentByAdhId
	 *
	 */
	@Transactional
	public Adherent findAdherentByAdhId(Integer adhId) throws DataAccessException {

		return findAdherentByAdhId(adhId, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhId
	 *
	 */

	@Transactional
	public Adherent findAdherentByAdhId(Integer adhId, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findAdherentByAdhId", adhId);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAdherentByAdhImmatriculation
	 *
	 */
	@Transactional
	public Set<Adherent> findAdherentByAdhImmatriculation(Integer adhImmatriculation) throws DataAccessException {

		return findAdherentByAdhImmatriculation(adhImmatriculation, -1, -1);
	}

	/**
	 * JPQL Query - findAdherentByAdhImmatriculation
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adherent> findAdherentByAdhImmatriculation(Integer adhImmatriculation, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdherentByAdhImmatriculation", startResult, maxRows, adhImmatriculation);
		return new LinkedHashSet<Adherent>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Adherent entity) {
		return true;
	}
}
